﻿using PMS.Data.Entities.Customers;
using PMS.Services.EntityServices;
using PMS.Services.Utils;
using PMS.Services.Utils.Events;

namespace PMS.Services.EventHandler.Caching
{
    /// <summary>
    /// Represents a customer cache event consumer
    /// </summary>
    public partial class CustomerCacheEventHandler 
        : CacheEventHandler<Customer>
        , IEventHandler<CustomerPasswordChangedEvent>
    {
        #region Methods

        /// <summary>
        /// Handle password changed event
        /// </summary>
        /// <param name="eventMessage">Event message</param>
        public void HandleEvent(CustomerPasswordChangedEvent eventMessage)
        {
            Remove(_cacheKeyService.PrepareKey(CacheKeyConfigs.CustomerPasswordLifetimeCacheKey, eventMessage.Password.CustomerId));
        }

        /// <summary>
        /// 分别针对Insert/Update/Delete进行操作，
        /// 如果操作逻辑相同，可以调用 ClearCache(Setting entity)
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="entityEventType"></param>
        protected override void ClearCache(Customer entity, EntityEventType entityEventType)
        {
            ClearCache(entity);
        }
        /// <summary>
        /// Clear cache data
        /// </summary>
        /// <param name="entity">Entity</param>
        protected override void ClearCache(Customer entity)
        {
            RemoveByPrefix(CacheKeyConfigs.CustomerCustomerRolesPrefixCacheKey);
            //RemoveByPrefix(CacheKeyConfigs.CustomerAddressesPrefixCacheKey);
            //RemoveByPrefix(CacheKeyConfigs.ShoppingCartPrefixCacheKey);
        }

        #endregion
    }

}
